<template>
  <a-modal
    title="客户"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" style="margin-bottom:8px" icon="redo" @click="getDataList()">刷新</a-button>

        <a-radio-group
          v-model="queryParam[1].keyword"
          style="margin-left: 8px"
          button-style="solid"
          @change="userChange"
        >
          <a-radio-button :value="null"> 全部</a-radio-button>
          <a-radio-button :value="-1"> 备案客户</a-radio-button>
          <a-radio-button :value="1"> 正式客户</a-radio-button>
          <a-radio-button :value="0"> 潜在客户</a-radio-button>
          <a-radio-button :value="2"> 休眠客户</a-radio-button>
        </a-radio-group>
      </div>

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam[0].keyword" placeholder="客户名称"/>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
              >查询
              </a-button>

              <a-button style="margin-left: 8px" @click="Repossess1">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :scroll="{ y: scroll }"
        :columns="columns"
        :rowKey="(row, index) => row.cCusCode"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: type }"
        :bordered="true"
        size="small"
      >
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      </a-table>

      <!-- <edit-form ref="editForm" :parentObj="this"></edit-form> -->
    </a-card>
  </a-modal>
</template>

<script>
// import EditForm from './EditForm'
// import List from '../Cust_CustomerLinkMan/List.vue'
import moment from 'moment'

const columns = [
  { title: '客户编码', dataIndex: 'cCusCode' },
  { title: '客户名称', dataIndex: 'cCusName' },
  { title: '销售经理', dataIndex: 'AllocationName' }
]

export default {
  components: {},
  props: {
    type: { type: String, default: 'radio', required: false }
  },

  created () {
  },
  computed: {
    scroll () {
      // //console.log()
      return Number(document.documentElement.clientHeight) * 0.45
    }
  },
  data () {
    return {
      user: 1,
      data: [],
      list: [], // 省级
      listChildren: [], // 市
      county: [], // 县
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: [{ condition: 'cCusName' }, { condition: 'cCusType', keyword: null }],
      selectedRows: [],
      selectedRowKeys: [],
      visible: false
    }
  },
  methods: {
    Repossess1 () {
      this.queryParam = [{ condition: 'cCusName' }, { condition: 'cCusType', keyword: null }]
      this.getDataList()
    },

    Repossess2 () {
      this.queryParam = [
        { condition: 'cCusName' },
        { condition: 'cProvince' },
        { condition: 'cCity' },
        { condition: 'cCounty' },
        { condition: 'AllocationStatus', keyword: 1 }
      ]
      this.getuserList()
    },
    userChange (n, b) {
      this.getDataList()
    },
    // 市级change
    city (e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then(resJson => {
        //console.log('res', resJson)
        this.loading = false
        this.listChildren = resJson.Data
        //console.log('333', this.listChildren)
      })
    },
    // 潜在客户
    getuserList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http

        .post('/CustomerManagement/Cust_CustomerInfoPotential/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    // 县change
    village (e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=' + e, {}).then(resJson => {
        //console.log('res', resJson)
        this.loading = false
        this.county = resJson.Data
        //console.log('333', this.county)
      })
    },
    // 获取省份信息
    getlist (e) {
      this.$http.post('/Base_BasicData/Base_District/GetDataListByPId?pId=0', {}).then(resJson => {
        this.loading = false
        this.list = resJson.Data
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    hasSelected () {
      return this.selectedRowKeys.length > 0
    },

    openChoose () {
      this.selectedRowKeys = []
      this.queryParam = [{ condition: 'cCusName' }, { condition: 'cCusType', keyword: null }]
      this.visible = true
    },
    handleChoose () {
      this.visible = false
      this.$emit('onChoose', this.selectedRows)
      this.queryParam = [{ condition: 'cCusName' }, { condition: 'cCusType', keyword: null }]
      this.pagination.current = 1

      this.getDataList()
    },

    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
